package com.letwal.cmcvnd.create

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.login.ReqLogin
import com.letwal.cmcvnd.aviewmodel.LoginViewModel
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.databinding.ActivityInputWalletVerifyBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*

/** 创建钱包-验证页 */
@SuppressLint("CustomSplashScreen")
class InputWalletVerifyActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityInputWalletVerifyBinding

    //登录viewModel
    private val mLoginViewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoginViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.create_wallet_verify.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityInputWalletVerifyBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //输入监听
            onHandleEditInputChanged()

            //监听live data 返回
            onHandleLiveDataCallback()

            //点击确定
            mActivityBinding.submitInputWalletVerify.setOnClickListener {
                //判断助记词是否为空
                val content = mActivityBinding.editInputWalletVerify.text.toString().trim()
                if (content.isNotBlank()) {
                    //去登录
                    //初始化配置参数
                    val email = intent.getStringExtra("email").toString()
                    val password = intent.getStringExtra("password").toString()
                    mLoginViewModel.login(
                        ReqLogin(
                            email = email,
                            password = password,
                            zjc = content
                        )
                    )
                } else {
                    Toast.makeText(
                        this,
                        getString(R.string.create_wallet_tips3),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 输入监听 */
    private fun onHandleEditInputChanged() {
        mActivityBinding.editInputWalletVerify.addTextChangedListener(object :
            TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(p0: Editable?) {
                //空格换成逗号
                val contentTemp = mActivityBinding.editInputWalletVerify.text.toString().trim()
                var content = contentTemp.replace(" ", ",")

                // 0123456789
                while (content.endsWith(",")) {
                    content = content.substring(0, content.length - 1)
                }

                //判断提交按键是否可用
                if (content.isNotBlank()) {
                    mActivityBinding.submitInputWalletVerify.setBackgroundResource(R.drawable.main_btn_hightlight)
                } else {
                    mActivityBinding.submitInputWalletVerify.setBackgroundResource(R.drawable.main_btn_ban)
                }
            }
        })
    }

    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {
        //登录回调
        mLoginViewModel.onLoginLiveData.observe(this) {
            if (it.isSuccess) {
                startToHomeActivity()
            }
        }
    }

}